package com.amber.proyecto.envia.imagenes.sw.usuario;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.amber.proyecto.envia.imagenes.sw.R;
import com.amber.proyecto.envia.imagenes.sw.utils.ParcheInputStream;
import com.amber.proyecto.envia.imagenes.sw.utils.Variables;

public class AdapterImagenesUsuario extends ArrayAdapter<ImagenNombre>
{
	private String ruta = "http://"+Variables.HOST+"/pags/";
	private ArrayList<ImagenNombre> imagenes = new ArrayList<ImagenNombre>();

	public AdapterImagenesUsuario(Context context, int textViewResourceId, ArrayList<ImagenNombre> objects)
	{
		super(context, textViewResourceId, objects);
		imagenes = objects;
	}

	public View getView(int position, View convertView, ViewGroup parent)
	{
		// holder pattern
		Holder holder = null;
		if (convertView == null)
		{
			holder = new Holder();

			LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = layoutInflater.inflate(R.layout.lvimagenesusuario, null);
			holder.setTextView1((TextView) convertView.findViewById(R.id.tvCatBuscaImagenesUsuario));
			holder.setIvFotoUsuario((ImageView)convertView.findViewById(R.id.ivFotoUsuario));
			
			convertView.setTag(holder);
		}
		else
		{
			holder = (Holder) convertView.getTag();
		}

		holder.getTextView1().setText(getItem(position).getComentario());
		
		String rutaUrl = ruta+imagenes.get(position).getIvFoto();
        HttpGet httpRequest = null;
        try {
                httpRequest = new HttpGet(rutaUrl);
        } catch (Exception e) {
                e.printStackTrace();
        }
        
        BitmapFactory.Options bfOpt = new BitmapFactory.Options();
        BufferedOutputStream bos = null;

		bfOpt.inScaled = true;
		bfOpt.inSampleSize = 80;
		bfOpt.inPurgeable = true;
		
		        
		HttpClient httpclient = new DefaultHttpClient();
		HttpResponse response;
		try {
			response = (HttpResponse) httpclient.execute(httpRequest);
			HttpEntity entity = response.getEntity();
			BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); 
			InputStream instream = bufHttpEntity.getContent();
			
			WeakReference<Bitmap> bm = new WeakReference<Bitmap>(BitmapFactory.decodeStream(new ParcheInputStream(instream),null,bfOpt));

					
			
			holder.getIvFotoUsuario().setImageBitmap(bm.get());
		} catch (ClientProtocolException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}
		
		//Bitmap bm = BitmapFactory.decodeStream(instream,null,bfOpt);

		return convertView;
	}
	/*
	private Bitmap decodeFile(File f){
	    Bitmap b = null;

	        //Decode image size
	    BitmapFactory.Options o = new BitmapFactory.Options();
	    o.inJustDecodeBounds = true;

	    FileInputStream fis = new FileInputStream(f);
	    BitmapFactory.decodeStream(fis, null, o);
	    fis.close();

	    int scale = 1;
	    if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
	        scale = (int)Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / 
	           (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
	    }

	    //Decode with inSampleSize
	    BitmapFactory.Options o2 = new BitmapFactory.Options();
	    o2.inSampleSize = scale;
	    fis = new FileInputStream(f);
	    b = BitmapFactory.decodeStream(fis, null, o2);
	    fis.close();

	    return b;
	}
	*/

}